//
//  MyPublishVC.m
//  TuYouAPP
//
//  Created by L on 14-2-26.
//
//

#import "MyPublishVC.h"
#import "DataModel.h"
#import "MyPublishCell.h"
#import "DynamicReplyVC.h"
#import "HisInfoVC.h"
#import "UIImageView+WebCache.h"
#import "Util.h"
@interface MyPublishVC ()
{
    NSMutableArray *tempArray;
}
@end

@implementation MyPublishVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)loadData
{
    [SVProgressHUD showWithStatus:@"加载中"];
    self.tableview.tableFooterView.hidden = YES;
    self.page =1;
   [self.user getMyPublish:self.page AnyCall:^(NSArray *array, NSString *eror) {
       [SVProgressHUD dismiss];
       [self.slimeView endRefresh];

       if (!eror) {
           self.page ++;
           tempArray = [[NSMutableArray alloc]init];

           [tempArray addObjectsFromArray:array];
           [self.tableview reloadData];
           self.tableview.tableFooterView.hidden = NO;
           if (array.count<ONE_PAGE) {
               self.refreshLabel.text = self.textNoMore;
           }
           else
           {
               self.refreshLabel.text = self.textPull;
               
           }
       }
       else
       {
           [SVProgressHUD showErrorWithStatus:eror];
       }
   }];
}

- (void)viewDidLoad
{
    self.mPageName = @"动态";
    [super viewDidLoad];
    self.haveFullupRefreshView = YES;
      [self initWithRefreshTableViewIos7:CGRectMake(0, 64, 320, DEVICE_Height-64) Ios6rect:CGRectMake(0, 44, 320, DEVICE_Height-64)];
    [self loadData];

    if (self.fromMyself) {
        self.Title = @"我的发布";
        self.LeftBarTitle  = @"我的";
    }
    else
    {
        self.Title = self.user.mUserDisplayName;
        self.LeftBarTitle  = @"返回";
        
        CGRect rect = self.navBar.rightBtn.frame;
        rect.origin.x += 5;
        rect.origin.y += 12;
        rect.size.height = 23;
        rect.size.width = 23;
        self.navBar.rightBtn.frame = rect;
        [self.navBar.rightBtn setImage:[UIImage imageNamed:@"hisPublish.png"] forState:UIControlStateNormal];

    }
    self.tableview.backgroundColor = ColorRGB(230, 230, 230);
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    // Do any additional setup after loading the view from its nib.
    TYUserState *aPublish = [[TYUserState alloc]init];
    aPublish.mImgArr = [[NSMutableArray alloc]init];
}
-(void)leftBtnTouched:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)rightBtnTouched:(id)sender
{
    HisInfoVC *vc = [[HisInfoVC alloc]initWithNibName:@"HisInfoVC" bundle:nil];
    
    vc.tagUser = self.user;
    [self.navigationController pushViewController:vc animated:YES];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return tempArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CustomCellIdentifier = @"MyPublishCell";
    
    MyPublishCell *cell = (MyPublishCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
    
    BOOL isUsed = NO;
    if (!isUsed &&! cell) {
        UINib *nib = [UINib nibWithNibName:@"MyPublishCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:@"MyPublishCell"];
        // isUsed = YES;
        cell = (MyPublishCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellIdentifier];
        
    }
    
    TYUserState *publish = [tempArray objectAtIndex:indexPath.row];
    
    //   [self.tableview reloadData];
    if (publish.mImgArr.count == 0 ||publish.mImgArr == nil) {
        for (int j =0; j<9; j++) {
            UIImageView *image = (UIImageView *)[cell viewWithTag:10+j];
            image.hidden = YES;
            
        }
    }
    else
    {
        for (int i = 0; i<publish.mImgArr.count; i++) {
            for (int j =0; j<9; j++) {
                UIImageView *image = (UIImageView *)[cell viewWithTag:10+j];
                
                if (j< publish.mImgArr.count) {
                    
                    image.hidden = NO;
                    //image.image = [publish.mImgArr objectAtIndex:j];
                    
                    NSString* ts = [publish.mImgArr objectAtIndex:j];
                    if( ts )
                    {
                        NSURL* turl  = [NSURL URLWithString:[Util makeSmallImgURL:ts]];
                        //NSLog(@"imgurl:%@",turl);
                        [image setImageWithURL: turl placeholderImage:S_D_IMG options:SDWebImageProgressiveDownload];
                        //   [image setImageWithURL:turl];
                        //[image setImageWithURL:turl placeholderImage:S_D_IMG];
                        
                        
                    }
                    else
                    {
                        image.image = S_D_IMG;
                    }
                }
                else
                {
                    image.hidden = YES;
                }
                //  image.image = [UIImage imageNamed:str];
                
            }
        }
    }

    [cell upDateCell:publish];
    
    cell.pubulishImage.layer.borderWidth = 0.5;
    cell.pubulishImage.layer.borderColor = [ColorRGB(212, 212, 212)CGColor];
       cell.publishAddr .text = publish.mPointInfo;
    cell.publishReplyCunt.text = publish.mReBackCount!=0? [NSString stringWithFormat:@"%d条回复",publish.mReBackCount]:@"暂无回复";
    NSMutableAttributedString *attString = [[NSMutableAttributedString alloc]initWithString:publish.mContext];
    NSInteger strLength = [publish.mContext length];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    [style setLineSpacing:2];
    
    [attString addAttribute:NSParagraphStyleAttributeName
                      value:style
                      range:NSMakeRange(0, strLength)];
    
    cell.publishContent.attributedText = attString;

    cell.publishTime.text = publish.mTimeText;
    
     cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // cell.textLabel.text = @"success";
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height+1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  //    [tableView deselectRowAtIndexPath:indexPath animated:YES];
        DynamicReplyVC *vc = [[DynamicReplyVC alloc]init];
        vc.isFromHis = YES;
    vc.thePublish = [tempArray objectAtIndex:indexPath.row];

        [self.navigationController pushViewController:vc animated:YES];
    
}
-(void)refresh
{
//    if (tempArray.count>0) {
//        return;
//    }
    [self.user getMyPublish:self.page AnyCall:^(NSArray *array, NSString *eror) {
        [self stopLoading];
        if (!eror) {
            self.tableview.tableFooterView.hidden = NO;
            if (array.count < ONE_PAGE) {
                self.refreshLabel.text = self.textNoMore;
             //   [SVProgressHUD showErrorWithStatus:@"没有更多数据"];
               // return ;
                [tempArray addObjectsFromArray:array];
                if (array.count != 0) {
                    [self.tableview reloadData];
                    self.page++;

                }
                return ;
            }
            self.refreshLabel.text = self.textPull;
            self.page++;
            [tempArray addObjectsFromArray:array];
            [self.tableview reloadData];

        }
        else
        {
            [SVProgressHUD showErrorWithStatus:eror];
        }
    }];


}
-(void)slimeRefreshStartRefresh:(SRRefreshView *)refreshView
{
        [self loadData];
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
